package com.ruoyi.userManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.mapper.BlindBoxMapper;
import com.ruoyi.blindBoxManage.mapper.BoxMapper;
import com.ruoyi.blindBoxManage.mapper.ClassMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.userManage.domain.DrawRecord;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.mapper.UserMapper;
import com.ruoyi.userManage.service.IDrawRecordService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 抽盒记录Controller
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/drawRecord/drawRecord")
public class DrawRecordController extends BaseController {
    @Autowired
    private IDrawRecordService drawRecordService;
    @Resource
    private ClassMapper classMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private BoxMapper boxMapper;
    /**
     * 查询抽盒记录列表
     */
    @PreAuthorize("@ss.hasPermi('drawRecord:drawRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrawRecord drawRecord) {
        if (!StrUtil.isEmpty(drawRecord.getClassName())) {
            Integer classId = classMapper.selectIdByName(drawRecord.getClassName());
            if (classId != null) {
                drawRecord.setClassId(classId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(drawRecord.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(drawRecord.getBlindBoxName());
            if (blindId != null) {
                drawRecord.setBlindBoxId(blindId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(drawRecord.getBoxName())) {
            Long boxId = boxMapper.selectIdByName(drawRecord.getBoxName());
            if (boxId != null) {
                drawRecord.setBoxId(boxId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(drawRecord.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(drawRecord.getUserPhone());
            if (userId != null) {
                drawRecord.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }

        if (drawRecord.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(drawRecord.getSixUserId());
            if (userId != null) {
                drawRecord.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<DrawRecord> list = drawRecordService.selectDrawRecordList(drawRecord);
        if (!CollectionUtils.isEmpty(list)) {
            for (DrawRecord record : list) {
                String blindBoxName = blindBoxMapper.selectNameById(record.getBlindBoxId());
                record.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String boxName = boxMapper.selectNameById(record.getBoxId());
                record.setBoxName(!StrUtil.isEmpty(boxName) ? boxName : "");
                String className = classMapper.selectNameById(record.getClassId());
                record.setClassName(!StrUtil.isEmpty(className) ? className : "");
                String phone = userMapper.selectPhoneById(record.getUserId());
                record.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                User user = userMapper.selectUserById(record.getUserId());
                record.setUserId(!ObjectUtils.isEmpty(user) ? user.getUserId() : 0);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出抽盒记录列表
     */
    @PreAuthorize("@ss.hasPermi('drawRecord:drawRecord:export')")
    @Log(title = "抽盒记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrawRecord drawRecord) {
        if (!StrUtil.isEmpty(drawRecord.getClassName())) {
            Integer classId = classMapper.selectIdByName(drawRecord.getClassName());
            if (classId != null) {
                drawRecord.setClassId(classId);
            }
        }
        if (!StrUtil.isEmpty(drawRecord.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(drawRecord.getBlindBoxName());
            if (blindId != null) {
                drawRecord.setBlindBoxId(blindId);
            }
        }
        if (!StrUtil.isEmpty(drawRecord.getBoxName())) {
            Long boxId = boxMapper.selectIdByName(drawRecord.getBoxName());
            if (boxId != null) {
                drawRecord.setBoxId(boxId);
            }
        }
        if (!StrUtil.isEmpty(drawRecord.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(drawRecord.getUserPhone());
            if (userId != null) {
                drawRecord.setUserId(userId);
            }
        }

        if (drawRecord.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(drawRecord.getSixUserId());
            if (userId != null) {
                drawRecord.setUserId(userId);
            }
        }
        List<DrawRecord> list = drawRecordService.selectDrawRecordList(drawRecord);
        if (!CollectionUtils.isEmpty(list)) {
            for (DrawRecord record : list) {
                String blindBoxName = blindBoxMapper.selectNameById(record.getBlindBoxId());
                record.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String boxName = boxMapper.selectNameById(record.getBoxId());
                record.setBoxName(!StrUtil.isEmpty(boxName) ? boxName : "");
                String className = classMapper.selectNameById(record.getClassId());
                record.setClassName(!StrUtil.isEmpty(className) ? className : "");
                String phone = userMapper.selectPhoneById(record.getUserId());
                record.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
            }
        }
        ExcelUtil<DrawRecord> util = new ExcelUtil<DrawRecord>(DrawRecord.class);
        util.exportExcel(response, list, "抽盒记录数据");
    }

    /**
     * 获取抽盒记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('drawRecord:drawRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(drawRecordService.selectDrawRecordById(id));
    }

    /**
     * 新增抽盒记录
     */
    @PreAuthorize("@ss.hasPermi('drawRecord:drawRecord:add')")
    @Log(title = "抽盒记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrawRecord drawRecord) {
        return toAjax(drawRecordService.insertDrawRecord(drawRecord));
    }

    /**
     * 修改抽盒记录
     */
    @PreAuthorize("@ss.hasPermi('drawRecord:drawRecord:edit')")
    @Log(title = "抽盒记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrawRecord drawRecord) {
        return toAjax(drawRecordService.updateDrawRecord(drawRecord));
    }

    /**
     * 删除抽盒记录
     */
    @PreAuthorize("@ss.hasPermi('drawRecord:drawRecord:remove')")
    @Log(title = "抽盒记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(drawRecordService.deleteDrawRecordByIds(ids));
    }
}
